<?php

namespace Acme\StaffBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class CommentController extends Controller {

  /**
   * @Route("/comment/", name="_comment")
   * @Template()
   */
  public function indexAction() {
    return array();
  }

  /**
   *
   * @Route("/save_staff_comment/{staff_id}", name="_save_staff_comment") 
   * 
   */
  public function saveStaffComentAction(\Symfony\Component\HttpFoundation\Request $request, $staff_id) {
    if ($request->getMethod() == 'POST') {
      $em = $this->getDoctrine()->getEntityManager();
      $staff = $em->getRepository('AcmeStaffBundle:Staff')->findOneById($staff_id);
      $comment = new \Acme\StaffBundle\Entity\Comment();
      $comment->setStaff($staff);
      $add_comment_form = $this->createForm(new \Acme\StaffBundle\Form\AddCommentForm(), $comment);
      $session = $request->getSession();
      $message = 'Отзыв не добавлен';
      $add_comment_form->bindRequest($request);
      if ($add_comment_form->isValid()) {
        $em->persist($comment);
        $em->flush();
        $message = 'Отзыв добавлен';
      }
      $session->setFlash('notice', $message);
      return $this->redirect($this->generateUrl('_view_staff', array('staff_id' => $staff_id)));
    }
    else {
      throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
    }
  }

  /**
   *
   * @Route("/save_staff_comment/{staff_id}/{comment_id}", name="_save_parent_comment") 
   * 
   */
  public function saveParentComentAction(\Symfony\Component\HttpFoundation\Request $request, $staff_id, $comment_id) {
    if ($request->getMethod() == 'POST') {
      $em = $this->getDoctrine()->getEntityManager();
      $parent_comment = $em->getRepository('AcmeStaffBundle:Comment')->findOneById($comment_id);
      $comment = new \Acme\StaffBundle\Entity\Comment();
      $comment->setParentComment($parent_comment);
      $add_comment_form = $this->createForm(new \Acme\StaffBundle\Form\AddCommentForm(), $comment);
      $session = $request->getSession();
      $message = 'Отзыв не добавлен';
      $add_comment_form->bindRequest($request);
      if ($add_comment_form->isValid()) {
        $em->persist($comment);
        $em->flush();
        $message = 'Отзыв добавлен';
      }
      $session->setFlash('notice', $message);
      return $this->redirect($this->generateUrl('_view_staff', array('staff_id' => $staff_id)));
    }
    else {
      throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
    }
  }
  
    /**
   *
   * @Route("/admin/save_staff_comment/{staff_id}", name="admin_save_staff_comment") 
   * 
   */
  public function adminSaveStaffComentAction(\Symfony\Component\HttpFoundation\Request $request, $staff_id) {
    if ($request->getMethod() == 'POST') {
      $em = $this->getDoctrine()->getEntityManager();
      $staff = $em->getRepository('AcmeStaffBundle:Staff')->findOneById($staff_id);
      $comment = new \Acme\StaffBundle\Entity\Comment();
      $comment->setStaff($staff);
      $add_comment_form = $this->createForm(new \Acme\StaffBundle\Form\AddCommentForm(), $comment);
      $session = $request->getSession();
      $message = 'Отзыв не добавлен';
      $add_comment_form->bindRequest($request);
      if ($add_comment_form->isValid()) {
        $em->persist($comment);
        $em->flush();
        $message = 'Отзыв добавлен';
      }
      $session->setFlash('notice', $message);
      return $this->redirect($this->generateUrl('admin_staff_edit', array('id' => $staff_id)));
    }
    else {
      throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
    }
  }

  /**
   *
   * @Route("/admin/save_staff_comment/{staff_id}/{comment_id}", name="admin_save_parent_comment") 
   * 
   */
  public function adminSaveParentComentAction(\Symfony\Component\HttpFoundation\Request $request, $staff_id, $comment_id) {
    if ($request->getMethod() == 'POST') {
      $em = $this->getDoctrine()->getEntityManager();
      $parent_comment = $em->getRepository('AcmeStaffBundle:Comment')->findOneById($comment_id);
      $comment = new \Acme\StaffBundle\Entity\Comment();
      $comment->setParentComment($parent_comment);
      $add_comment_form = $this->createForm(new \Acme\StaffBundle\Form\AddCommentForm(), $comment);
      $session = $request->getSession();
      $message = 'Отзыв не добавлен';
      $add_comment_form->bindRequest($request);
      if ($add_comment_form->isValid()) {
        $em->persist($comment);
        $em->flush();
        $message = 'Отзыв добавлен';
      }
      $session->setFlash('notice', $message);
      return $this->redirect($this->generateUrl('admin_staff_edit', array('id' => $staff_id)));
    }
    else {
      throw new \Symfony\Component\HttpKernel\Exception\NotFoundHttpException();
    }
  }
  
  /**
   * @Route("/admin/delete_comment/{comment_id}", name="admin_delete_comment")
   * @param \Symfony\Component\HttpFoundation\Request $request
   * @param type $comment_id 
   */
  public function adminDeleteCommentAction(\Symfony\Component\HttpFoundation\Request $request, $comment_id) {
    if ($request->getMethod() == 'POST') {
      $em = $this->getDoctrine()->getEntityManager();
      $comment = $em->getRepository('AcmeStaffBundle:Comment')->findOneById($comment_id);
      $em->remove($comment);
      $em->flush();

    }
     return new \Symfony\Component\HttpFoundation\Response('', 200);
  }
  
  /**
   * @Route("/admin/update_comment/{comment_id}", name="admin_update_comment")
   * @param \Symfony\Component\HttpFoundation\Request $request
   * @param type $comment_id 
   */
  public function adminUpdateCommentAction(\Symfony\Component\HttpFoundation\Request $request, $comment_id) {
    if ($request->getMethod() == 'POST') {
      $em = $this->getDoctrine()->getEntityManager();
      $comment = $em->getRepository('AcmeStaffBundle:Comment')->findOneById($comment_id);
      $add_comment_form = $this->createForm(new \Acme\StaffBundle\Form\AddCommentForm(), $comment);
      $add_comment_form->bindRequest($request);
      if ($add_comment_form->isValid()) {
        $em->persist($comment);
        $em->flush();
        $staff_id = $comment->getStaff()->getId();
       return $this->redirect($this->generateUrl('admin_staff_edit', array('id' => $staff_id)));
      }
     return new \Symfony\Component\HttpFoundation\Response('', 200);
    }
  }

}